﻿using PersistenceLayer.DAL;
using SupportLayer.Constantes.OUTROS;
using SupportLayer.Objetos.Outros;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Util;

namespace ByBikeShop.Administracao.Pages
{
	public partial class Banner : BaseAdmin
	{
		private int NumImages = 0;

		protected void Page_Load(object sender, EventArgs e)
		{
			base.PageLoad(true);
		}

		public override void CarregaDados()
		{

		}

		public override void CarregaGrid()
		{
		}

		public override void PopulaGrid()
		{
			DataTable dt = new DataTable();
			dt.Columns.Add("ID");
			dt.Columns.Add("Imagem_Url");

			int count = 0;
			foreach (PersistenceLayer.Model.Banner b in new BannerDAL().Listar())
			{
				dt.Rows.Add(b.ID, ResolveUrl(b.Imagem_Url));
				count++;
			}

			NumImages = count;

			grdBanners.DataSource = dt;
			grdBanners.DataBind();

		}

		protected void grdBanners_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				if (e.Row.RowIndex == 0)
					e.Row.Cells[2].FindControl("btnUp").Visible = false;

				if (e.Row.RowIndex == NumImages - 1)
					e.Row.Cells[2].FindControl("btnDown").Visible = false;

				ImageButton btnAtivo = (ImageButton)e.Row.FindControl("btnAtivo");

				if (new BannerDAL().Obter(Convert.ToInt32(btnAtivo.CommandArgument)).Ativo)
					btnAtivo.ImageUrl = "/Imagens/Icones/ativo.png";
				else
					btnAtivo.ImageUrl = "/Imagens/Icones/inativo.png";

			}
		}

		protected void grdBanners_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			int ID = Gridview.GetID(sender, e);

			switch (e.CommandName)
			{
				case "up":
					new BannerDAL().ChangeOrders(ID, true);
					break;
				case "down":
					new BannerDAL().ChangeOrders(ID, false);
					break;
				case "deletar":
					BannerDAL bd = new BannerDAL();
					bd.Deletar(bd.Obter(ID));
					break;
				default:
					break;
			}
			Response.Redirect("/Administracao/Pages/Banner.aspx");
		}

		protected void btnEnviar_Click(object sender, EventArgs e)
		{
			try
			{
				HttpFileCollection hfc = Request.Files;

				if (Request.Files.Count <= 0)
					throw new ApplicationException("Escolha um arquivo!");

				if ((fupBanner.FileName.EndsWith(".jpg")) ||
						(fupBanner.FileName.EndsWith(".png")) ||
						(fupBanner.FileName.EndsWith(".jpeg")))
				{
					HttpPostedFile hpf = hfc[0];

					if (hpf.ContentLength > 0)
					{
						string guid = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(fupBanner.FileName);
						hpf.SaveAs(Server.MapPath("~/Imagens/Banner/" + guid));

						Response.Redirect("/Administracao/Pages/Preview.aspx?Imagem=" + guid);
					}
				}
				else throw new ApplicationException("Só são aceitas imagens do tipo jpg e png.");

			}
			catch (Exception ex)
			{
				lblErro.Text = ex.Message;
			}
		}

		protected void btnAtivo_Command(object sender, CommandEventArgs e)
		{

		}
	}
}